package com.riko.comic.core.config;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.riko.comic.module.sys.bean.po.User;
import com.riko.comic.module.sys.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service("userDetail")
public class CustomUserDetailService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class).eq(User::getUsername,username));

        if (user == null) {
            throw new RuntimeException("账户不存在");
        }


        return new UserDetail(user.getId(), username, user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList(""));
    }
}